<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="20">
          <el-form-item>
            <el-input
              v-model="searchForm.fuzzy"
              style="width:250px"
              placeholder="仪器名称/仪器编号/仪器型号"
              clearable
              @keyup.enter.native="handleSearch"
            />
          </el-form-item>

          <el-form-item prop="type" label="仪器类别">
            <el-select
              v-model="searchForm.type"
              style="width: 145px"
              placeholder="请选择仪器类别"
              clearable
            >
              <el-option
                v-for="item in typleList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="classType" label="检测类别">
            <el-select
              v-model="searchForm.classType"
              style="width: 145px"
              placeholder="请选择检测类别"
              clearable
            >
              <el-option
                v-for="item in classTypeList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="iconfont icon-chaxun1"
              @click="handleSearch"
            >
              查询</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="4" class="search-right">
          <el-button type="text" icon="el-icon-refresh" @click="handleSearch"
            >刷新</el-button
          >
        </el-col>
      </el-row>
    </el-form>
    <div>
      <el-table
        stripe
        border
        :data="orderData"
        highlight-current-row
        :header-cell-style="{ background: '#ECF3FC' }"
        style="width: 100%"
      >
        <template slot="empty">
          <div class="table_empty">
            <div class="empty_tips">
              <span>
                暂无分包信息
              </span>
            </div>
          </div>
        </template>
        <el-table-column
          type="index"
          width="55"
          label="序号"
          fixed="left"
          align="center"
        />
        <el-table-column
          prop="name"
          label="项目名称"
          fixed="left"
          min-width="120px"
          align="center"
        />
        <el-table-column
          prop="model"
          label="仪器型号"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="position"
          label="放置地点"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="lastCheck"
          label="上次期间核查日期"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="checkCycle"
          label="核查周期"
          width="140"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.checkCycle == '1'">一个月</span>
            <span v-if="scope.row.checkCycle == '2'">三个月</span>
            <span v-if="scope.row.checkCycle == '3'">半年</span>
            <span v-if="scope.row.checkCycle == '4'">一年</span>
            <span v-if="scope.row.checkCycle == '5'">俩年</span>
            <span v-if="scope.row.checkCycle == '6'">三年</span>
            <span v-if="scope.row.checkCycle == '7'">无需检定</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="checkDate"
          label="本次期间核查日期"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="checkUser"
          label="核查人"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="checkCharge"
          label="部门负责人"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="checkType"
          label="核查方式"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="checkName"
          label="标准器具/物质名称"
          width="180px"
          align="center"
        />
        <el-table-column
          prop="result"
          label="出入库状态"
          width="100"
          align="center"
        >
          <template slot-scope="scope">
            <span
              v-if="scope.row.result == '1'"
              style="color: green; font-weight: bold"
              >合格</span
            >
            <span
              v-if="scope.row.result == '2'"
              style="color: red; font-weight: bold"
              >不合格</span
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="130">
          <template slot-scope="scope">
            <el-button type="text" @click="View(scope.row)"> 查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 查看 -->
    <el-dialog
      title="查看"
      :visible.sync="MyInstrumentView"
      center
      top="5vh"
      width="1000px"
    >
      <div>
        <div class="install-title">期间核查详情信息</div>
        <el-form label-width="150px">
          <el-row :gutter="12">
            <el-col :span="8">
              <el-form-item prop="name" label="仪器名称:">
                {{ ViewForm.name }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="model" label="仪器型号:">
                {{ ViewForm.model }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="position" label="放置地点:">
                {{ ViewForm.position }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="上次期间核查日期:" prop="lastCheck">
                {{ ViewForm.lastCheck }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="checkCycle" label="核查周期:">
                <el-form-item>
                  <span v-if="ViewForm.checkCycle == '1'">一个月</span>
                  <span v-if="ViewForm.checkCycle == '2'">三个月</span>
                  <span v-if="ViewForm.checkCycle == '3'">半年</span>
                  <span v-if="ViewForm.checkCycle == '4'">一年</span>
                  <span v-if="ViewForm.checkCycle == '5'">两年</span>
                  <span v-if="ViewForm.checkCycle == '6'">三年</span>
                  <span v-if="ViewForm.checkCycle == '7'">无需检定</span>
                </el-form-item>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="本次期间核查日期:" prop="checkDate">
                {{ ViewForm.checkDate }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="checkUser" label="核查人:">
                {{ ViewForm.checkUser }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="checkCharge" label="部门负责人:">
                {{ ViewForm.checkCharge }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="checkType" label="核查方式:">
                {{ ViewForm.checkType }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="checkName" label="标准器具/物质名称:">
                {{ ViewForm.checkName }}
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item prop="result," label="出入库状态:">
                <el-form-item>
                  <span v-if="ViewForm.result == '1'">合格</span>
                  <span v-if="ViewForm.result == '2'">不合格</span>
                </el-form-item>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-dialog>
    <div style="float: right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"
      />
    </div>
  </div>
</template>

<script>
import * as orderServer from "@/api/offer";
import * as Checklist from "@/api/Checklist";
import { findValidList } from "@/api/dict";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
const ACCOUNT_STATUS = {
  "0": "待处理",
  "1": "已处理"
};
const CHECK_STATUS = {
  "0": "单次",
  "1": "周",
  "2": "月",
  "3": "季度",
  "4": "半年",
  "5": "年"
};
export default {
  components: { Pagination },
  filters: {
    statusQualityFilter(status) {
      const statusMap = {
        "0": "",
        "1": "success"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      ACCOUNT_STATUS,
      CHECK_STATUS,
      orderData: [],
      ViewForm: [],
      MyInstrumentView: false,
      searchForm: {
        pageNumber: 1,
        pageSize: 10
      },
      total: 0,
      approveVisible: false,
      approveForm: {
        result: "0",
        remark: ""
      },
      orderDetailForm: {
        offerId: "",
        offerBaseVo: {
          consignorName: "",
          consignorLinker: "",
          consignorLinkerPhone: "",
          createUserName: "",
          finishDate: "",
          remark: "",
          checkCircle: "",
          checkTaskCount: ""
        },
        factorTdos: [],
        costVo: {},
        oncePrice: "",
        selfCostVos: []
      },
      detailVisible: false,
      factorData: [],
      enableFactorData: [], // 无能力
      ableFactorData: [], // 有能力
      outFactorData: [],
      multipleSelection: [],
      multipleEnableSelection: [],
      judgeId: "",
      judgeArray: [],
      showJudgeArray: [],
      currentRow: {},
      ableFactor: "",
      unAbleFactor: "",
      typleList: [
        {
          name: "现场采样仪器",
          id: 1
        },
        {
          name: "现场检测仪器",
          id: 2
        },
        {
          name: "实验室仪器",
          id: 3
        },
        {
          name: "校准仪器",
          id: 4
        }
      ],
      classTypeList: [
        {
          name: "水",
          id: 1
        },
        {
          name: "气",
          id: 2
        },
        {
          name: "噪声",
          id: 3
        },
        {
          name: "土壤、底泥、固废、污泥和生活垃圾",
          id: 4
        },
        {
          name: "样品制备",
          id: 5
        },
        {
          name: "小型分析仪器",
          id: 6
        },
        {
          name: "色谱仪",
          id: 7
        },
        {
          name: "光谱仪",
          id: 8
        },
        {
          name: "质谱仪",
          id: 9
        }
      ]
    };
  },
  computed: {
    ableFactorDt() {
      const search = this.ableFactor.trim();
      if (search) {
        return this.ableFactorData.filter(
          i => i.factorName.indexOf(search) > -1
        );
      }
      return this.ableFactorData;
    },
    unAbleFactorDt() {
      const search = this.unAbleFactor.trim();
      if (search) {
        return this.enableFactorData.filter(
          i => i.factorName.indexOf(search) > -1
        );
      }
      return this.enableFactorData;
    }
  },
  created() {
    this.getOutSourceCom();
    this.handleSearch();
  },
  methods: {
    handleSearch() {
      console.log(123);
      Checklist.periodHistoryList(this.searchForm).then(res => {
        if (res.code === 200) {
          this.orderData = res.result.records;
          this.total = res.result.total;
        }
      });
    },
    getOutSourceCom() {
      findValidList().then(res => {
        this.judgeArray = res.result;
      });
    },
    handleDetail(row) {
      this.currentRow = Object.assign({}, row);
      // 获取数据
      orderServer.findJudgeOfferFactorQryVoByOfferId(row.offerId).then(res => {
        this.showJudgeArray = res.result;
        this.detailVisible = true;
      });
    },

    addJudge() {
      if (this.ableFactorData.length - this.multipleSelection.length <= 0) {
        this.$message.error("有能力检测因子项不能全部分包");
        return;
      }
      if (
        this.judgeId &&
        (this.multipleSelection || this.multipleEnableSelection)
      ) {
        const evel = this.outFactorData.find(i => i.judgeId === this.judgeId);
        const jevel = this.judgeArray.find(i => i.id === this.judgeId);
        let factorInfos = this.multipleSelection;
        factorInfos = factorInfos.concat(this.multipleEnableSelection);
        if (!evel) {
          this.outFactorData.push({
            judgeId: this.judgeId,
            judgeName: jevel.judgeOrganName,
            facotrs: factorInfos
          });
        } else {
          evel.facotrs = evel.facotrs.concat(factorInfos);
        }
        // 处理成功移除 选中的记录
        this.multipleSelection.forEach(i => {
          var index = this.ableFactorData.indexOf(i);
          if (index !== -1) {
            this.ableFactorData.splice(index, 1);
          }
        });
        this.multipleEnableSelection.forEach(i => {
          var index = this.enableFactorData.indexOf(i);
          if (index !== -1) {
            this.enableFactorData.splice(index, 1);
          }
        });
        this.multipleSelection = [];
        this.multipleEnableSelection = [];
      } else {
        this.$message.warning("请先选择分包机构,且因子不能为空");
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSelectionEnableChange(val) {
      this.multipleEnableSelection = val;
    },
    delJudge(item, index) {
      if (item.facotrs) {
        const local = [...item.facotrs];
        this.outFactorData.splice(index, 1);
        local.forEach(i => {
          if (
            this.currentRow.certificationType != null &&
            this.currentRow.certificationType != ""
          ) {
            // 1.CMA 2.CNAS
            if (this.currentRow.certificationType === "1") {
              if (i.cmaFlg === "0") {
                // 无能力
                this.enableFactorData.push(i);
              } else {
                // 有能力
                this.ableFactorData.push(i);
              }
            } else {
              if (i.cnasFlg === "0") {
                // 无能力
                this.enableFactorData.push(i);
              } else {
                // 有能力
                this.ableFactorData.push(i);
              }
            }
          }
          // if (i.extAssistFlg === '1') {
          //   this.enableFactorData.push(i)
          // } else {
          //   this.ableFactorData.push(i)
          // }
        });
      }
    },
    confirmDialog() {
      if (this.enableFactorData.length > 0) {
        this.$message.error("无能力检测因子需要全部分包");
        return;
      }
      var tdoList = this.ableFactorData.map(i => {
        return {
          planFactorId: i.planFactorId,
          organId: ""
        };
      });
      this.outFactorData.forEach(i => {
        if (i.facotrs) {
          i.facotrs.forEach(j => {
            tdoList.push({
              planFactorId: j.planFactorId,
              organId: i.judgeId
            });
          });
        }
      });
    },
    // 查看
    View(row) {
      console.log(row);
      this.MyInstrumentView = true;
      this.ViewForm = row;
    }
  }
};
</script>

<style scoped lang="scss" type="text/scss">
.clearfix {
  line-height: 40px;
  display: flex;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.clearfix {
  line-height: 40px;
  display: flex;
}
.install-title {
  display: flex;
  align-items: center;
  line-height: 40px;
  padding-left: 20px;
  color: rgb(15, 14, 14);
  background-color: #d7dae2;
  justify-content: flex-start;
  margin-top: 15px;
}
.delete {
  width: 10px;
  height: 10px;
  position: relative;
  top: -10px;
  cursor: pointer;
}
::v-deep .el-upload-list {
  width: 100px;
  display: flex;
  margin: 10px 10px;
}
::v-deep .el-upload-list > li {
  margin: 10px 10px;
}
::v-deep.el-upload-list__item {
  margin: 10px 10px;
}
::v-deep.el-upload-list--picture .el-upload-list__item {
  margin: 10px 10px !important;
}
.user-info-head {
  position: relative;
  display: inline-block;
  height: 120px;
}

::v-deep.avatar-upload-preview {
  width: 400px;
  border-radius: 0%;
  height: 400px;
  > img {
    transform: translateX(0);
  }
}
.user-info-head:hover:after {
  content: "+";
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  color: #eee;
  background: rgba(0, 0, 0, 0.5);
  font-size: 24px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  cursor: pointer;
  line-height: 110px;
  border-radius: 50%;
}
</style>
